package com.lv.suyiyong.hfr.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.priceSmallDot
import com.lv.common.utils.MoneyUtil
import com.lv.service.bean.GoodsItemBean
import com.lv.suyiyong.hfr.R

/**
 * 作者： ygx
 * 创建日期：2025/8/11 下午2:10
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：TODO
 */
class SpecialScrollChildAdapter(
    private val context: Context,
    private var list: List<GoodsItemBean>
) :
    RecyclerView.Adapter<SpecialScrollChildAdapter.ViewHolder>() {

    private var itemCount = Int.MAX_VALUE

    fun isAutoScroll(isAutoScroll: Boolean) {
        itemCount = if (isAutoScroll) Int.MAX_VALUE else list.size
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_home_scroll_child, parent, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.initData(this.list[position % this.list.size])
    }

    override fun getItemCount(): Int {
        return if (list.size > 4) itemCount else list.size
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun initData(item: GoodsItemBean) {
            itemView.findViewById<ImageView>(R.id.iv_img)?.LoadSrc(item.image)
            val price = item.reducedprice
            var priceStr = MoneyUtil.moneydiv("$price", "100").priceSmallDot()
            if (priceStr == "0") {
                priceStr = "面议"
            } else {
                priceStr = "￥$priceStr"
            }
            itemView.findViewById<TextView>(R.id.tv_price)?.text = priceStr
        }
    }
}
